home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / tutorials / custEducation / opengl1 / examples / irisgl_vs_opengl / tesselate.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-11-11  |  4.9 KB  |  216 lines

  1. /*
  2.  *  tesselate.c  - open a window, clear the background, and render 
  3.  *    a simple nonconvex polygon with and without tesselation.
  4.  *    The untesselated polygon is drawn (incorrectly) on the left.
  5.  *    The corresponding tesselated object is drawn on the right.
  6.  *
  7.  *  The tesselated object is stored in a display list.
  8.  *
  9.  *  Invoking this program with any argument causes the callbacks to
  10.  *  produce debugging output.
  11.  */
  12. #include <stdio.h>
  13. #include <GL/gl.h>
  14. #include <GL/glu.h>
  15. #include <aux.h>
  16.  
  17. /*  Function Prototypes  */
  18.  
  19. GLvoid  initialize( int, char ** );
  20. GLvoid  drawScene( GLvoid );
  21. GLvoid  resize( GLsizei, GLsizei );
  22.  
  23. void tesserror( GLenum errno );
  24. void my_glBegin( GLenum mode);
  25. void my_glEnd( void);
  26. void my_glVertex2fv( const GLfloat *v);
  27.  
  28. #define Dprintf if (debug) printf
  29. int debug;
  30.  
  31.  
  32. void
  33. main( int argc, char *argv[] )
  34. {
  35.     initialize( argc, argv );
  36.  
  37.     auxMainLoop( drawScene );
  38. }
  39.  
  40. #define NPOLYVERTS (sizeof(polyVerts)/sizeof(polyVerts[0]))
  41. #define COLOR_OFFSET 2 /* color offset in array of info about vertex */
  42. #define VSIZE 5
  43. /*      X  Y  R  G  B */
  44. static GLfloat polyVerts[][VSIZE]={
  45.     {  -2,  2, 0, 0, 0 },
  46.     {  -2, -2, 0, 0, 1 },
  47.     {   2, -2, 0, 1, 0 },
  48.     {   2,  2, 0, 1, 1 },
  49.     {   0,  0, 1, 0, 0 }
  50. };
  51.  
  52. static GLUtriangulatorObj *tObj = NULL;
  53. static GLuint tObjList;
  54.  
  55. GLvoid
  56. initialize( int argc, char *argv[] )
  57. {
  58.     GLsizei    width, height, winSize;
  59.  
  60.     int i;
  61.     GLdouble vertex[3];
  62.  
  63.     if (argc > 1)
  64.         debug = 1;
  65.     else
  66.         debug = 0;
  67.  
  68.     auxGetScreenSize( &width, &height );
  69.     winSize = ( width < height ) ? width : height;
  70.     auxInitPosition( winSize / 4, winSize / 4, winSize / 2,
  71.         winSize / 2 );
  72.     auxInitDisplayMode( AUX_RGBA );
  73.     auxInitWindow( argv[0] );
  74.  
  75.     auxReshapeFunc( resize );
  76.     auxKeyFunc( AUX_ESCAPE, auxQuit );
  77.  
  78.     glClearColor( 0.0, 0.0, 1.0, 1.0 );
  79.  
  80.     glPointSize( 3.5 );
  81.     glLineWidth( 2.5 );
  82.  
  83.     /* Create the tesselation object */
  84.     tObj = gluNewTess();
  85.     gluTessCallback(tObj, GLU_BEGIN, my_glBegin);
  86.     gluTessCallback(tObj, GLU_VERTEX, my_glVertex2fv);
  87.     gluTessCallback(tObj, GLU_END, my_glEnd);
  88.     gluTessCallback(tObj, GLU_ERROR, tesserror);
  89.  
  90.     tObjList = glGenLists(1);
  91.     glNewList( tObjList, GL_COMPILE );
  92.         gluBeginPolygon(tObj);
  93.             for ( i = 0; i < NPOLYVERTS; i++ ) {
  94.                 vertex[0] = polyVerts[i][0];
  95.                 vertex[1] = polyVerts[i][1];
  96.                 vertex[2] = 0;
  97.                 /* vertex[] is the vertex of the polygon 
  98.                  * being tesselated. polyVerts[i] is 
  99.                  * the vertex plus associated data
  100.                  * (color, in this case) that get passed 
  101.                  * (by the GLU_VERTEX callback) to the 
  102.                  * my_glVertex2fv func. */
  103.                 gluTessVertex(tObj, vertex, &polyVerts[i]);
  104.             }
  105.         gluEndPolygon(tObj);
  106.     glEndList();
  107.  
  108.     glMatrixMode( GL_PROJECTION );
  109.     /* Set up the world coordinates we want */
  110.     glOrtho( -10.0, 10.0, -10.0, 10.0, -1.0, 1.0 );
  111.     glMatrixMode( GL_MODELVIEW );
  112. }
  113.  
  114. GLvoid
  115. resize( GLsizei width, GLsizei height )
  116. {
  117.     GLdouble    aspect, left, right, bottom, top;
  118.  
  119.     glViewport( 0, 0, width, height );
  120.  
  121.     /* compute aspect ratio */
  122.     aspect = (GLdouble) width / (GLdouble) height;
  123.  
  124.     /* make sure the window goes from [-10.0, 10.0] in the
  125.           smallest dimension */
  126.     if ( aspect < 1.0 ) {
  127.         left = -10.0;
  128.         right = 10.0;
  129.         bottom = -10.0 * ( 1.0 / aspect );
  130.         top = 10.0 * ( 1.0 / aspect );
  131.     } else {
  132.         left = -10.0 * aspect;
  133.         right = 10.0 * aspect;
  134.         bottom = -10.0;
  135.         top = 10.0;
  136.     }
  137.  
  138.     glMatrixMode( GL_PROJECTION );
  139.     /* Reset world coordinates first ... */
  140.     glLoadIdentity();
  141.  
  142.     /* Then set them to what we want based on the new aspect ratio*/
  143.     glOrtho( left, right, bottom, top, -1.0, 1.0 );
  144.     glMatrixMode( GL_MODELVIEW );
  145. }
  146.  
  147. GLvoid
  148. drawScene( GLvoid )
  149. {
  150.     int i;
  151.  
  152.     glClear( GL_COLOR_BUFFER_BIT );
  153.  
  154.     /* Draw untesselated object (incorrectly) */
  155.     glPushMatrix();
  156.         glTranslatef( -5.0, 0.0, 0.0 );
  157.         glBegin( GL_POLYGON );
  158.             for (i=0; i < NPOLYVERTS; i++) {
  159.                 glColor3fv( polyVerts[i]+COLOR_OFFSET );
  160.                 glVertex2fv( polyVerts[i] );
  161.             }
  162.         glEnd();
  163.     glPopMatrix();
  164.  
  165.     /* Draw tesselated objects (correctly) */
  166.     glPushMatrix();
  167.         glTranslatef( 5.0, 0.0, 0.0 );
  168.         glCallList( tObjList );
  169.     glPopMatrix();
  170.  
  171.     glFlush();
  172.     auxCheckError("drawScene");
  173. }
  174.  
  175. /* The next four routines are for tesselation callbacks. */
  176.  
  177. /* Called if there is an error during tesselation. */
  178. void 
  179. tesserror ( GLenum errno ) 
  180. {
  181.     printf( "tesselation error: %s\n", gluErrorString( errno ) );
  182.     fflush( stdout );
  183.     auxQuit();
  184. }
  185.  
  186. /* Called when a new primitive is started. */
  187. void 
  188. my_glBegin(GLenum mode)
  189. {
  190.     Dprintf ("my_glBegin: mode is %d\n", mode);
  191.     glBegin (mode);
  192. }
  193.  
  194. /* Called when a primitive is finished. */
  195. void 
  196. my_glEnd(void)
  197. {
  198.     Dprintf ("my_glEnd:\n");
  199.     glEnd();
  200. }
  201.  
  202. /* Called when a vertex of a primitive is generated by the 
  203.  * tesselator.  The parameter v is a pointer to the data 
  204.  * that was passed to the GLU_VERTEX callback.  In this case,
  205.  * the first two elements are coordinates of the vertex 
  206.  * and the next three specify the vertex color. */
  207. void 
  208. my_glVertex2fv(const GLfloat *v)
  209. {
  210.     Dprintf ("my_glVertex2fv: v[0] = %g, v[1] = %g ", v[0], v[1]);
  211.     Dprintf ("R = %g, G = %g, B = %g\n", v[2], v[3], v[4]);
  212.  
  213.     glColor3fv(v+COLOR_OFFSET);
  214.     glVertex2fv (v);
  215. }
  216.